package co.fitstart.weapp.api.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dao;

import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.course.schedule.ScheduleStatus;
import co.fitstart.entity.user.inbody.InbodyRecord;
import co.fitstart.entity.utils.DateFormatUtils;
import co.fitstart.entity.utils.DateUtils;
import co.fitstart.weapp.api.dao.CourseScheduleDao;
import co.fitstart.weapp.api.dao.InbodyDao;
import co.fitstart.weapp.api.service.InbodyRecordService;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Service("inbodyRecordService")
public class InbodyRecordServiceImpl implements InbodyRecordService {

    @Dao
    private CourseScheduleDao courseScheduleDao;

    @Dao
    private InbodyDao inbodyDao;

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.InbodyRecordService#list(co.fitstart.entity.course.round.CourseRound)
     */
    public List<InbodyRecord> list(CourseRound courseRound) {
        List<CourseSchedule> courseSchedules = courseScheduleDao.list(courseRound, ScheduleStatus.FINISH);
        
        if(courseSchedules.isEmpty()) {
            return null;
        } else {
            Date begin = DateUtils.parseDate(DateFormatUtils.format(courseSchedules.get(courseSchedules.size() - 1).getOccurDate(), "yyyy-MM-dd") + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
            Date end = DateUtils.parseDate(DateFormatUtils.format(courseSchedules.get(0).getOccurDate(), "yyyy-MM-dd") + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
            
            return inbodyDao.list(courseRound.getStudent().getUser(), begin, end);
        }
        
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.InbodyRecordService#list(co.fitstart.entity.course.round.CourseRound, java.util.List)
     */
    public List<InbodyRecord> list(CourseRound courseRound, List<CourseSchedule> courseSchedules) {
        if(courseSchedules.isEmpty()) {
            return null;
        } else {
            Date begin = DateUtils.parseDate(DateFormatUtils.format(courseSchedules.get(courseSchedules.size() - 1).getOccurDate(), "yyyy-MM-dd") + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
            Date end = DateUtils.parseDate(DateFormatUtils.format(courseSchedules.get(0).getOccurDate(), "yyyy-MM-dd") + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
            
            return inbodyDao.list(courseRound.getStudent().getUser(), begin, end);
        }
    }

}
